<?php
if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
require_once dirname(__FILE__).'/plugin_erling_account_center.class.php';

class plugin_erling_account_center_member extends plugin_erling_account_center {
    public function logging_top() {
        static $executed = false;
        if($executed) return '';
        $executed = true;
        global $_G;
        
        $table_connfig = C::t('#erling_account_center#er_account_config');
        $table_connfig_data = $table_connfig->fetch_by_keys(['appid','redirect_url','only_acount_auth']);
        $appid = $table_connfig_data['appid']['value'];
        $domain = $table_connfig_data['redirect_url']['value'];
        $redirect_url = $domain.'/plugin.php?id=erling_account_center:redirect_callback';
        $state = md5(uniqid(mt_rand(), true));
        // http://account-center.cn,https://user.20idc.com
        $http = 'https://user.20idc.com/authorize/login?type=login&' . 'domain='.$domain.'&appid='.$appid.'&redirect_url='.$redirect_url.'&response_type=code&scope=get_user_info&state='.$state;

        $html = '<style>
            .login-buttons {
                margin: 15px 0;
                text-align: center;
            }
            .aggregate-login {
                display: inline-block;
                margin: 0 120px;
                padding: 10px 25px;
                background: linear-gradient(135deg, #4c78af, #3a5f8d);
                color: white;
                border: none;
                border-radius: 5px;
                font-size: 14px;
                font-weight: bold;
                cursor: pointer;
                box-shadow: 0 2px 5px rgba(0,0,0,0.2);
                transition: all 0.3s ease;
            }
            .aggregate-login:hover {
                background: linear-gradient(135deg, #3a5f8d, #2c4a6e);
                transform: translateY(-2px);
                box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            }
        </style>';
        $html .= '<div class="login-buttons">';
        // $html .= '<button class="default-login" onclick="switchToDefaultLogin()">默认方式登录</button>';
        $html .= '<button class="aggregate-login" onclick="switchToAggregateLogin()">聚合账号登录</button>';
        $html .= '</div>';
        
        $html .= '<script>
            function switchToAggregateLogin() {
                var http = "'.$http.'";
                window.open(http, "_blank");
                return;
            }
        </script>';

        if($table_connfig_data['only_acount_auth']['value'] == 1){
            $html .= <<<EOF
                <script>
                    // 处理主页面登录表单
                    function hideLoginForms() {
                        // 主页面表单
                        var loginForm = document.querySelector('form[name="login"]');
                        if(loginForm) loginForm.style.display = 'none';
                        
                        // 弹窗表单
                        var fastloginForm = document.getElementById('fastloginform');
                        if(fastloginForm) fastloginForm.style.display = 'none';
                    }
                    
                    // 登录页
                    hideLoginForms();
                    document.addEventListener('DOMContentLoaded', function() {
                        var loginForm = document.querySelector('form[name="login"]');
                        if(loginForm) {
                            loginForm.style.display = 'none';
                        }
                    });
                    
                    // 监听弹窗打开事件
                    if(typeof showWindow !== 'undefined') {
                        var originalShowWindow = showWindow;
                        showWindow = function() {
                            originalShowWindow.apply(this, arguments);
                            setTimeout(hideLoginForms, 500); // 延迟执行确保弹窗已加载
                        };
                    }
                    
                    // 监听AJAX内容加载
                    if(typeof ajaxget !== 'undefined') {
                        var originalAjaxget = ajaxget;
                        ajaxget = function() {
                            originalAjaxget.apply(this, arguments);
                            setTimeout(hideLoginForms, 500);
                        };
                    }
                </script>
            EOF;
        }
        
        return $html;
    }

    // public function register_side_top(){
    //     static $executed = false;
    //     if($executed) return '';
    //     $executed = true;


    //     $table_connfig = C::t('#erling_account_center#er_account_config');
    //     $table_connfig_data = $table_connfig->fetch_by_keys(['appid','redirect_url']);
    //     $appid = $table_connfig_data['appid']['value'];
    //     $domain = $table_connfig_data['redirect_url']['value'];
    //     $redirect_url = $domain.'/plugin.php?id=erling_account_center:redirect_callback';
    //     $state = md5(uniqid(mt_rand(), true));
    //     $http1 = 'https://user.20idc.com/authorize/login?type=register&' . 'domain='.$domain.'&appid='.$appid.'&redirect_url='.$redirect_url.'&response_type=code&scope=get_user_info&state='.$state;
    //     $http2 = 'https://user.20idc.com/authorize/register?type=register&' . 'domain='.$domain.'&appid='.$appid.'&redirect_url='.$redirect_url.'&response_type=code&scope=get_user_info&state='.$state;
    //     // type=register注册新用户
    //     // 简化HTML结构并确保闭合标签正确
    //     $html = '<button onclick="window.open(\''.$http1.'\', \'_blank\')" 
    //                 style="padding:5px;background:#4CAF50;color:white;border:none;cursor:pointer;border-radius:5px;margin-right: 5px;">
    //             使用聚合账号登录自动注册
    //         </button><button onclick="window.open(\''.$http2.'\', \'_blank\')" 
    //                 style="padding:5px;background:#4CAF50;color:white;border:none;cursor:pointer;border-radius:5px;margin-right: 5px;">
    //             聚合账号注册
    //         </button>';

    //     return $html;
    // }
    
    public function register_bottom() {
        static $executed = false;
        if($executed) return '';
        $executed = true;

        $table_connfig = C::t('#erling_account_center#er_account_config');
        $table_connfig_data = $table_connfig->fetch_by_keys(['only_acount_auth','appid','redirect_url']);
        $only_acount_auth = $table_connfig_data['only_acount_auth']['value'];
        
        if($only_acount_auth == 1) {

            $appid = $table_connfig_data['appid']['value'];
            $domain = $table_connfig_data['redirect_url']['value'];
            $redirect_url = $domain.'/plugin.php?id=erling_account_center:redirect_callback';
            $state = md5(uniqid(mt_rand(), true));
            $http1 = 'https://user.20idc.com/authorize/login?type=register&' . 'domain='.$domain.'&appid='.$appid.'&redirect_url='.$redirect_url.'&response_type=code&scope=get_user_info&state='.$state;
            $http2 = 'https://user.20idc.com/authorize/register?type=register&' . 'domain='.$domain.'&appid='.$appid.'&redirect_url='.$redirect_url.'&response_type=code&scope=get_user_info&state='.$state;
            // type=register注册新用户
            // 简化HTML结构并确保闭合标签正确
            $html = '<div style="text-align:center; margin:15px 0;">';
            $html .= '<button onclick="window.open(\''.$http1.'\', \'_blank\')" 
                        style="padding:10px 15px;background:#4c78af;color:white;border:none;cursor:pointer;border-radius:5px;margin:0 5px;min-width:180px;">
                    使用聚合账号登录自动注册
                </button>';
            $html .= '<button onclick="window.open(\''.$http2.'\', \'_blank\')" 
                        style="padding:10px 15px;background:#4CAF50;color:white;border:none;cursor:pointer;border-radius:5px;margin:0 5px;min-width:180px;">
                    聚合账号注册
                </button>';
            $html .= '</div>';

            $html .= '
                <script>
                    function defaultRegistNone() {
                        var defaultForm = document.querySelector("form[name=register]") || document.querySelector(".registerform");
                        if(defaultForm) defaultForm.style.display = "none";
                    }
                    document.addEventListener("DOMContentLoaded", function() {
                        defaultRegistNone();
                    });
                </script>
            ';
        } else {
            $html = '';
        }

        return $html;
    }


}